--- 'https://github.com/Frege/frege/issues/20  Better java support' 
module tests.comp.Issue20 where

import Java.Util

native module 
  -- type Object
  -- interface Comparator Int, Comparable String 
  where { 
    // Be careful with comments, no unpaired braces, please!
    // And also no pre- or post-decrement, because this would be a Frege comment!
    // You can write \u002d\u002d if you must.

    public static class FComp<A> implements java.util.Comparator<A> {
        final private Func.U<A, Func.U<A, Short>> f;
        final public int compare(final A arg$1, final A arg$2) {
            return Prelude.IEnum_Ordering.ord(
                f.apply(Thunk.<A>lazy(arg$1)).call()
                 .apply(Thunk.<A>lazy(arg$2)).call()
                ) - 1;
        }
        public FComp(Func.U<A, Func.U<A, Short>> x) { f = x; }
        public static<A> java.util.Comparator<A> mk(Func.U<A, Func.U<A, Short>> f) { 
            return new FComp<A>(f); 
        }
        public int compareTo(Object other) { return -1; }
    }
}

pure native mkComparator FComp.mk{a} :: (a -> a -> Ordering) -> Comparator a

compareFst :: (Int, a) -> (Int, a) -> Ordering
compareFst = comparing fst

main = do
    let stringComparator = mkComparator (String.<=>) 
        tupleComparator  = mkComparator compareFst
    println (stringComparator.compare "foo" "bar")
    println (tupleComparator.compare (42, "foo") (43, "bar"))
